﻿using MediatR;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Quickly.Identity.Domain;

namespace Quickly.Identity.Services.Manage;

public record QueryUsersCommand(Paging Paging);

public class QueryUsers
{
    public static async Task<PagedList<TUser>> ExecuteAsync<TUser>(
        QueryUsersCommand command, 
        IQueryableUserStore<TUser> queryableUserStore, 
        CancellationToken cancellationToken)
        where TUser : class
    {
        cancellationToken.ThrowIfCancellationRequested();

        var totalCount = await queryableUserStore.Users.CountAsync(cancellationToken);
        var users = new PagedList<TUser>(command.Paging, totalCount);
        users.Datas.AddRange(await queryableUserStore.Users.TakePage(command.Paging).AsNoTracking().ToListAsync(cancellationToken));
        return users;
    }
}